package utils

import (
	"log"
	"os"

	"github.com/joho/godotenv"
)

func LoadEnv() {
	err := godotenv.Load(".env")
	if err != nil {
		log.Println("---failed to load .env config file---")
		log.Println("---loaded default settings---")
	}
	setDefaultEnv()
}

func setDefaultEnv() {
	var DB_DSN, JWT_SECRET string
	DB_DSN = "Postgres"
	JWT_SECRET = "Token***"
	if _, exists := os.LookupEnv("APP_PORT"); !exists {
		os.Setenv("APP_PORT", "8080")
	}

	if _, exists := os.LookupEnv("JWT_SECRET"); !exists {
		os.Setenv("JWT_SECRET", "1234567890")
		JWT_SECRET = "Default value 1234567890"
	}

	if _, exists := os.LookupEnv("DB_DSN"); !exists {
		os.Setenv("DB_DSN", "inmemory")
		DB_DSN = "inmemory SQLite"
	}

	if _, exists := os.LookupEnv("SU_LOGIN"); !exists {
		os.Setenv("SU_LOGIN", "admin")
	}

	if _, exists := os.LookupEnv("SU_PASS"); !exists {
		os.Setenv("SU_PASS", "admin")
	}

	if _, exists := os.LookupEnv("USER_ROLES"); !exists {
		os.Setenv("USER_ROLES", "admin,user")
	}

	log.Printf(`OIO-Static_Server settings:
		APP_PORT = %s
		JWT_SECRET = %s
		DataBase = %s
		SuperUser = %s
	`, os.Getenv("APP_PORT"), JWT_SECRET, DB_DSN, os.Getenv("SU_LOGIN"))
}
